/* $Id: ClientDAOImpl.java 
 * Copyright (C) 2011, TRAKHEES
 *  
 * Revision History
 *
 * Date  			Author 		    	Remarks
 * ---------------------------------------------------------------------------------
 *
 */
package ae.pcfc.etks.online.common.dao.provider;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import ae.pcfc.etks.online.common.dao.ClientDAO;
import ae.pcfc.etks.online.common.domain.Client;
import ae.pcfc.etks.online.common.vo.TrakheesIdVO;

@Repository
public class ClientDAOImpl extends BaseDAO implements ClientDAO {


	/**
	 * add
	 * 
	 * @see ae.pcfc.etks.online.common.dao.ClientDAO#add(ae.pcfc.etks.online.common.domain.Client)
	 * 
	 */
	public void add(Client client) {

		getHibernateTemplate().getSessionFactory().getCurrentSession().save(client);
	}

	/**
	 * findClientByUserName
	 * 
	 * @see ae.pcfc.etks.online.common.dao.ClientDAO#findClientByUserName(java.lang.String)
	 * 
	 */
	public Client findClientByUserName(String userName) {

		Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(Client.class, "client");
		criteria.createAlias("client.users", "user");
		criteria.add(Restrictions.eq("user.UUsername", userName).ignoreCase());

		Client client = (Client) criteria.uniqueResult();

		return (client == null) ? null : client;
	}

	/**
	 * update
	 * 
	 * @see ae.pcfc.etks.online.common.dao.ClientDAO#update(ae.pcfc.etks.online.common.domain.Client)
	 * 
	 */
	public void update(Client client) {

		getHibernateTemplate().getSessionFactory().getCurrentSession().saveOrUpdate(client);

	}

	/**
	 * findTrakheesIdByUserName
	 * 
	 * @see ae.pcfc.etks.online.common.dao.ClientDAO#findTrakheesIdByUserName(java.lang.String)
	 * 
	 */
	public String findTrakheesIdByUserName(String userName) {
		Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(Client.class, "client");
		criteria.createAlias("client.users", "user");
		criteria.add(Restrictions.eq("user.UUsername", userName).ignoreCase());

		Client client = (Client) criteria.uniqueResult();

		return (client == null) ? null : client.getCTrakheesId();
	}


	/**
	 * findByPrimaryKey
	 * 
	 * @see ae.pcfc.etks.online.common.dao.ClientDAO#findByPrimaryKey(java.lang.Long)
	 * 
	 */
	public Client findByPrimaryKey(Long id) {

		return (Client) getHibernateTemplate().getSessionFactory().getCurrentSession().get(Client.class, id);
	}


	/**
	 * findClientUserByEmail
	 * 
	 * @see ae.pcfc.etks.online.common.dao.ClientDAO#findClientUserByEmail(java.lang.String)
	 * 
	 */
	public Client findClientUserByEmail(String email) {
		
		Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(Client.class, "client");
		criteria.createAlias("client.users", "user");
		criteria.add(Restrictions.eq("user.UEmail", email).ignoreCase());

		@SuppressWarnings("unchecked")
		List<Client> client =  criteria.list();

		return (client != null && client.size() > 0 ) ? client.get(0) : null;
	}


	/**
	 * findByTrakheesIds
	 * 
	 * @see ae.pcfc.etks.online.common.dao.ClientDAO#findByTrakheesIds(java.util.List)
	 * 
	 */
	public List<Client> findByTrakheesIds(List<TrakheesIdVO> trakheesIdVOs) {
		
		List<String> strings = new ArrayList<String>();
		for(TrakheesIdVO trakheesIdVO: trakheesIdVOs){
			strings.add(trakheesIdVO.getpTrakheesId());
		}
		
		Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(Client.class, "client");
		criteria.add(Restrictions.in("CTrakheesId", strings));
		
		@SuppressWarnings("unchecked")
		List<Client> clients =  criteria.list();
		
		return (clients != null && clients.size() > 0 ) ? clients : null;
	}


	/**
	 * findByTrakheesId
	 * 
	 * @see ae.pcfc.etks.online.common.dao.ClientDAO#findByTrakheesId(java.lang.String)
	 * 
	 */
	public Client findByTrakheesId(String trakheesId) {
		
		Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(Client.class, "client");
		criteria.add(Restrictions.eq("CTrakheesId", trakheesId).ignoreCase());
		
		@SuppressWarnings("unchecked")
		List<Client> clients =  criteria.list();
		
		return (clients != null && clients.size() > 0 ) ? clients.get(0) : null;
	}

	/*
	 * 
	 * public ClientReq getClientByApplicationId(Long applicationId) { Criteria
	 * criteria =
	 * getHibernateTemplate().getSessionFactory().getCurrentSession().
	 * createCriteria(ClientReq.class);
	 * criteria.add(Restrictions.eq("applicationId", applicationId));
	 * 
	 * @SuppressWarnings("unchecked") List<ClientReq> list = criteria.list();
	 * if(list.size() > 0) return list.get(0); else return null; }
	 */
}
